package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.UserPage;
import com.itheima.pojo.BsUser;
import com.itheima.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService userService;

    //用户修改
    @PutMapping("/{id}")
    public void modify(@PathVariable Long id, @RequestBody BsUser bsUser) {
        userService.update(id, bsUser);
    }

    //用户列表
    @GetMapping
    public UserPage findList(Long page, Long pagesize, String username) {
        UserPage result = userService.findList(page, pagesize, username);
        return result;
    }

    //用户添加
    @PostMapping
    public void addUser(@RequestBody BsUser bsUser) {
        userService.add(bsUser);
    }

    //用户删除
    @DeleteMapping("/{id}")
    public void removeById(@PathVariable Long id) {
        userService.deleteById(id);
    }

    @PutMapping("/{id}/{disabled}")
    public void modifyStatus(@PathVariable Long id, @PathVariable boolean disabled) {
        userService.updateStatus(id, disabled);
    }

    //用户简单列表
    @GetMapping("/simple")
    public List simpleList(String keyword, Integer disabled) {
        List list =userService.simpleList(keyword,disabled);
        return list;
    }

    //用户详情
    @GetMapping("/{id}")
    public BsUser findById(@PathVariable Long id){
        return userService.findById(id);
    }

    //用户重置密码
/*
    @PutMapping("/{id}")
    public void modifyPassword(@PathVariable Long id,String password){
        System.out.println(id);
        System.out.println(password);
        userService.modifyPassword(id,password);
    }
*/

    @PostMapping("/{id}/{password}")
    public void modifyPwd(@PathVariable Long id,@PathVariable String password){
        System.out.println(id);
        System.out.println(password);
        userService.modifyPassword(id,password);
    }
}
